package staff;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class ask extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlbirth = new JLabel("出 生 年 月：");
    JLabel jleducation= new JLabel("学      历：");
    JLabel jlpolitics = new JLabel("政 治 面 貌：");
    JLabel jlmarriage = new JLabel( "婚 姻 状 况：");
    JLabel jlphone = new JLabel("联 系 电 话：");
    JLabel jlsection= new JLabel("部      门：");
    JLabel jlwork = new JLabel("职      务：");

    JTextField jtpno = new JTextField("",20);

    JLabel jname = new JLabel();
    JLabel jsex = new JLabel();
    JLabel jbirth = new JLabel();
    JLabel jeducation = new JLabel();
    JLabel jpolitics = new JLabel();
    JLabel jmarriage = new JLabel();
    JLabel jphone = new JLabel();
    JLabel jsection = new JLabel();
    JLabel jwork = new JLabel();

    JButton buttonask = new JButton("查询");
    JButton buttonreturn = new JButton("返回");

    public ask() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpbirth = new JPanel();
        JPanel jpeducation = new JPanel();
        JPanel jppolitics = new JPanel();
        JPanel jpmarriage = new JPanel();
        JPanel jpphone = new JPanel();
        JPanel jpsection = new JPanel();
        JPanel jpwork= new JPanel();
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jname);

        jpsex.add(jlsex);
        jpsex.add(jsex);

        jpbirth.add(jlbirth);
        jpbirth.add(jbirth);

        jpeducation.add(jleducation);
        jpeducation.add(jeducation);

        jppolitics.add(jlpolitics);
        jppolitics.add(jpolitics);

        jpmarriage.add(jlmarriage);
        jpmarriage.add(jmarriage);

        jpphone.add(jlphone);
        jpphone.add(jphone);

        jpsection.add(jlsection);
        jpsection.add(jsection);

        jpwork.add(jlwork);
        jpwork.add(jwork);


        jpforbutton.add(buttonask);
        jpforbutton.add(buttonreturn);
        buttonask.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                String xpno = jtpno.getText();
                Connection Test = null;
                ResultSet res = null;
                Statement stat = null;

                String sql = "SELECT pno,name,xingbie,birth,xueli,politics,marriage,phone,section,zhiwu FROM allstaff where pno='"+xpno+"';";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("jdbc haha");
                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                try{
                    Test=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
                    stat=Test.createStatement();
                    res=stat.executeQuery(sql);
                    while (res.next()) {
                        String i = res.getString(1).trim();

                        if (i.equals(jtpno.getText())) {
                            jname.setText(res.getString(2));
                            jsex.setText(res.getString(3));
                            jbirth.setText(res.getString(4));
                            jeducation.setText(res.getString(5));
                            jpolitics.setText(res.getString(6));
                            jmarriage.setText(res.getString(7));
                            jphone.setText(res.getString(8));
                            jsection.setText(res.getString(9));
                            jwork.setText(res.getString(10));
                            break;
                        } else {
                            if (e.getSource() == buttonask) {
                                javax.swing.JOptionPane.showMessageDialog(null, "不存在！查询失败！");
                            }
                            break;
                        }
                    }
                }catch (SQLException e1) {
                    e1.printStackTrace();


                }
                finally{
                    try{
                        Test.close();

                    }catch(SQLException ar){
                        ar.printStackTrace();
                    }

                }}}

        );
        buttonreturn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){dispose();
            }
        });
        this.setTitle("教师信息");
        this.setLayout(new GridLayout(11,1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirth);
        this.add(jpeducation);
        this.add(jppolitics);
        this.add(jpmarriage);
        this.add(jpphone);
        this.add(jpsection);
        this.add(jpwork);

        this.add(jpforbutton);
        this.setLocation(300,300);
        this.setSize(350,300);
        this.setVisible(true);

    }
}
